import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:lcapp/basic/address_info.dart';
import 'package:lcapp/widget/asr_app_bar.dart';

class XianHuoSXLXPage extends StatefulWidget {
  final int sxType;
  final Function callBack;
  final List selectIds;
  final double topSpace;
  const XianHuoSXLXPage({Key? key, required this.callBack, required this.sxType, required this.selectIds, required this.topSpace}) :  super(key: key);

  @override
  State<XianHuoSXLXPage> createState() => _XianHuoSXLXPageState();
}

class _XianHuoSXLXPageState extends State<XianHuoSXLXPage> {
  late int sxType;
  List list = [];
  List selects = [];
  List selectIds = [];
  int maxSelCount = 1;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    sxType = widget.sxType;
    selectIds = widget.selectIds??[];
    if(sxType==0){
      getPlList();
    }else if(sxType==1){
      list = [
        {'name':'全部','id':'-1'},
        {'name':'出售','id':'0'},
        {'name':'采购','id':'1'},
      ];
      if(selectIds.isEmpty){
        selectIds.add('-1');
      }
    }else if(sxType==2){
      list = [
        {'name':'全部','id':'0'},
        {'name':'每斤/价格','id':'1'},
        {'name':'每吨/价格','id':'2'},
      ];
      if(selectIds.isEmpty){
        selectIds.add('0');
      }
    }
  }

  getPlList()async{
    list = await AddressInfo.getPlList();
    maxSelCount = list.length;
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.transparent,
      child: Column(
        children: [
          GestureDetector(
            onTap: (){
              SmartDialog.dismiss();
            },
            child: Container(
              height: widget.topSpace,
              color: Colors.transparent,
            ),
          ),
          Container(
            height: 1,
            color: Color(0xffE9E8E9),
          ),
          Container(
            width: 1.sw,
            height: 150.w,
            color: Colors.white,
            padding: EdgeInsets.symmetric(horizontal: 30.w),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(
                  height: 70.w,
                  child: ListView.builder(
                      padding: EdgeInsets.zero,
                      scrollDirection: Axis.horizontal,
                      itemCount: list.length,
                      itemBuilder: (context,index){
                        return GestureDetector(
                          onTap: (){
                            setState(() {
                              if(selectIds.contains(list[index]['id'])){
                                selectIds.remove(list[index]['id']);
                              }else {
                                if(maxSelCount == 1){
                                  selectIds.clear();
                                }
                                selectIds.add(list[index]['id']);
                              }
                            });
                          },
                          child: Container(
                            padding: EdgeInsets.symmetric(horizontal: 40.w,),
                            margin: EdgeInsets.only(right: 20.w),
                            alignment: Alignment.center,
                            decoration: BoxDecoration(
                                color: selectIds.contains(list[index]['id'])?Color(0x2fEB6E30): Color(0xffECECEC),
                                borderRadius: BorderRadius.circular(70.w),
                            ),
                            child: Text(list[index]['name'],style: TextStyle(
                              color:selectIds.contains(list[index]['id'])?Color(0xffEB6E30): Color(0xff232323),
                              fontSize: 28.sp
                            ),),
                          ),
                        );
                      }),
                )
              ],
            ),
          ),
          Container(
            height: 1,
            color: Color(0xffE9E8E9),
          ),
          Container(
            color: Colors.white,
            padding: EdgeInsets.symmetric(vertical: 20.w),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                GestureDetector(
                  onTap: (){
                    if(widget.callBack!=null){
                      selectIds.clear();
                      widget.callBack(selects);
                    }
                    SmartDialog.dismiss();
                  },
                  child: Container(
                    width:350.w,
                    height: 90.w,
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                        color: Color(0xffEBEBEB),
                        borderRadius: BorderRadius.circular(10.w)
                    ),
                    child: Text('重置',style: TextStyle(
                        color: Color(0xff232323),
                        fontSize:30.sp,
                        fontWeight: FontWeight.w500
                    ),),
                  ),
                ),
                GestureDetector(
                  onTap: (){
                    if(widget.callBack!=null){
                      for (var id in selectIds){
                        for (var item in list){
                          if(id == item['id']&&item['name']!='全部'){
                            selects.add(item);
                          }
                        }
                      }
                      widget.callBack(selects);
                    }
                    SmartDialog.dismiss();
                  },
                  child: Container(
                    width:320.w,
                    height: 80.w,
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                        color: Color(0xffFA6E28),
                        borderRadius: BorderRadius.circular(10.w)
                    ),
                    child: Text('完成',style: TextStyle(
                        color: Colors.white,
                        fontSize:30.sp,
                        fontWeight: FontWeight.w500
                    ),),
                  ),
                )
              ],
            ),
          ),
          Expanded(child: GestureDetector(
            onTap: (){
              SmartDialog.dismiss();
            },
            child: Container(
              width: 1.sw,
              color: Color.fromRGBO(0, 0, 0, 0.25),
            ),
          ))
        ],
      ),
    );
  }
}
